<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :titles="['流行','新款','精选']"
                 @tabclick="tabclick"
                 ref="tabcontrol1"
                 v-show="istabFixed"
                 class="tabcontrol"/>
    <scroll class="content"
            ref="scroll"
            :probe-type="3"
            :pull-up-load="true"
            @pullingUp="loadmore"
            @scroll="contentscroll">
      <home-swiper :banners="banners"
                   @swiperImageLoad="swiperImageLoad"/>
      <recommend-view :recommends="recommends"/>
      <Feature/>
      <tab-control :titles="['流行','新款','精选']"
                   @tabclick="tabclick"
                   ref="tabcontrol2"/>
      <goodslist :goods="showGoods"/>
    </scroll>
    <back-top @click.native="backclick"
              v-show="isShowBackTop"/>
  </div>
</template>

<script>
import homeSwiper from "./childComponents/homeSwiper";
import RecommendView from "./childComponents/RecommendView";
import Feature from "./childComponents/Feature";

import NavBar from "components/common/navbar/NavBar";
import tabControl from "components/content/tabControl/tabControl";
import goodslist from "components/content/goods/goodslist";
import scroll from "components/common/scroll/scroll";
import {itemListenerMixin,backtopMixin} from "common/mixin";
import {getHomeMultidata, getHomeGoods} from "network/home";



export default {
  name: "home",
  components: {
    NavBar,
    homeSwiper,
    RecommendView,
    Feature,
    tabControl,
    goodslist,
    scroll
  },
  mixins:[itemListenerMixin,backtopMixin],
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        'pop': {page: 0, list: []},
        'new': {page: 0, list: []},
        'sell': {page: 0, list: []}
      },
      currentType: 'pop',
      taboffsetTop: 0,
      istabFixed: false,
      saveY: 0,
    }
  },
  created() {
    this.getHomeMultidata()
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')
  },
  mounted() {
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  methods: {
    getHomeMultidata() {
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list
        this.recommends = res.data.recommend.list
      })
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list)
        this.goods[type].page += 1

        this.$refs.scroll.finishPullup()
      })
    },
    tabclick(index) {
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      this.$refs.tabcontrol1.currentIndex = index
      this.$refs.tabcontrol2.currentIndex = index
    },
    contentscroll(position) {
      this.isShowBackTop = position.y < -1000;
      this.istabFixed = position.y < -this.taboffsetTop
    },
    loadmore() {
      this.getHomeGoods(this.currentType)
    },
    swiperImageLoad() {
      this.taboffsetTop = this.$refs.tabcontrol2.$el.offsetTop;
    }
  },
  deactivated() {
    this.$bus.$off('itemImageLoad',this.homeItemListener)
  },
  // activated() {
  //   this.$refs.scroll.scrollTo(0,this.saveY,0)
  //   this.$refs.scroll.Refresh
  // }
}
</script>

<style scoped>
#home {
  /*padding-top: 44px;*/
  height: 100vh;
  /*position: relative;*/
}

.home-nav {
  background-color: var(--color-tint);
  color: white;

  /*position: fixed;*/
  /*left: 0;*/
  /*right: 0;*/
  /*top: 0;*/
  /*z-index: 9;*/
}

/*.content {*/
/*  height: calc(100% - 93px);*/
/*  overflow: hidden;*/
/*  !*margin-top: 44px;*!*/
/*}*/
.content {
  overflow: hidden;

  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}

.tabcontrol {
  position: relative;
  z-index: 9;
}
</style>
